package com.appclub.kitsilano.bulletin;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import com.appclub.kitsilano.R;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;

public class PDFdownloader extends Service {
	String i;
	int month;
	int ii = 0;
	int b = 3;
	String PREFS_NAME;
	SharedPreferences settings;
	SharedPreferences.Editor editor;
	boolean wifi = true;
	private BroadcastReceiver receiver;

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		PREFS_NAME = "Kitsilano";
		settings = getSharedPreferences(PREFS_NAME, 0);
		editor = settings.edit();

		try {
			Bundle extras = intent.getExtras();
			b = extras.getInt("Bulletin");
		} catch (Exception e) {
		}

		if (b == 1) {
			Calendar c = Calendar.getInstance();
			month = c.get(Calendar.MONTH);
			String extStorageDirectory = Environment
					.getExternalStorageDirectory().toString();
			final File folder = new File(extStorageDirectory + "/Kitsilano/");
			if (!folder.exists()) {
				folder.mkdir();
			}
			new Thread() {
				public void run() {
					try {
						String[] month_a = getResources().getStringArray(
								R.array.months2);
						for (int ii = 0; ii <= 31; ii++) {
							i = String.valueOf(ii);
							if (i.length() == 1) {
								i = "0" + String.valueOf(ii);
							}
							File file = new File(folder, month_a[month] + " "
									+ i + ".pdf");
							if (!file.exists()) {
								file.createNewFile();
							}
							downloader.DownloadFile(
									"http://go.vsb.bc.ca/schools/kitsilano/Students/student_bulletin/Documents/"
											+ month_a[month] + "%20" + i
											+ ",%202013.pdf", file);
							if (ii == 31) {
								if (b == 1) {
									Intent intent1 = new Intent(
											PDFdownloader.this, Bulletin.class);
									intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									startActivity(intent1);
								}
							}
						}
					} catch (IOException e) {
					}
					boolean state_setting2 = settings.getBoolean("Autoupdate",
							false);
					if (state_setting2 == false) {
						stopSelf();
					}
				}
			}.start();
		}
		if (b == 3) {
			boolean state_setting2 = settings.getBoolean("Autoupdate", false);
			if (state_setting2 == false) {
				stopSelf();
			}
			receiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					String action = intent.getAction();
					if (action.equals(Intent.ACTION_TIME_TICK)) {
						Calendar c = Calendar.getInstance();
						int H = c.get(Calendar.HOUR_OF_DAY);
						int D = c.get(Calendar.DAY_OF_WEEK);
						if (H == 7 || H == 8 || H == 14 || H == 15) {
							ii = ii + 1;
							if (ii == 15) {
								ii = 0;
								boolean state_setting3 = settings.getBoolean(
										"Autoupdatewifi", false);
								ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
								NetworkInfo info = conMan
										.getActiveNetworkInfo();
								WifiManager wifi_state = (WifiManager) getSystemService(Context.WIFI_SERVICE);

								if (state_setting3) {
									if (info.isAvailable()) {
										if (wifi_state.isWifiEnabled()) {
											wifi = true;
										} else {
											wifi = false;
										}
									}
								} else {
									wifi = true;
								}
								if (info.isAvailable() && wifi) {
									month = c.get(Calendar.MONTH);
									String extStorageDirectory = Environment
											.getExternalStorageDirectory()
											.toString();
									final File folder = new File(
											extStorageDirectory + "/Kitsilano/");
									if (!folder.exists()) {
										folder.mkdir();
									}

									String[] month_a = getResources()
											.getStringArray(R.array.months2);
									File file = new File(
											folder,
											month_a[c.get(Calendar.MONTH)]
													+ " "
													+ c.get(Calendar.DAY_OF_MONTH)
													+ ".pdf");
									if (!file.exists()
											|| D != Calendar.SATURDAY
											|| D != Calendar.SUNDAY) {
										new Thread() {
											public void run() {
												String[] month_a = getResources()
														.getStringArray(
																R.array.months2);
												for (int ii = 0; ii <= 31; ii++) {
													i = String.valueOf(ii);
													if (i.length() == 1) {
														i = "0"
																+ String.valueOf(ii);
													}
													final File file = new File(
															folder,
															month_a[month]
																	+ " " + i
																	+ ".pdf");
													try {
														if (!file.exists()) {
															file.createNewFile();
														}
													} catch (IOException e) {
														e.printStackTrace();
													}
													downloader
															.DownloadFile(
																	"http://go.vsb.bc.ca/schools/kitsilano/Students/student_bulletin/Documents/"
																			+ month_a[month]
																			+ "%20"
																			+ i
																			+ ",%202013.pdf",
																	file);
												}
											}
										}.start();
									}
								}
							}
						}
					}
				}
			};
			IntentFilter intentFilter = new IntentFilter(
					Intent.ACTION_TIME_TICK);
			registerReceiver(receiver, intentFilter);
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onDestroy() {
		this.unregisterReceiver(receiver);
		super.onDestroy();
	}
}